TomatoBreak = @TomatoBreak

USER_NAME_EXPRESSION = ///^([a-zA-Z0-9_\-])*$///
EMAIL_EXPRESSION = ///^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$///

class User extends Spine.Model

    @configure 'User', 'userName', 'password', 'confirm', 'email'

    @extend Spine.Model.Ajax

    @extend { url: -> 'user' }

    validate: ->

        errors = []

        addError = (name, message) ->
            error = { name: name, messages: [] }
            error.messages.push message
            errors.push error

        unless @userName
            addError 'userName', 'User name cannot be blank.'
        else if not USER_NAME_EXPRESSION.test @userName
            addError 'userName', 'User name must be alphanumeric characters and 
                can only include dash or underscore.'

        addError 'password', 'Password cannot be blank.' unless @password

        unless @confirm
            addError 'confirm', 'Confirm password cannot be blank.'
        else if @confirm isnt @password
            addError 'confirm', 'Confirm password does not match password.'

        unless @email
            addError 'email', 'Email cannot be blank.'
        else if not EMAIL_EXPRESSION.test @email
            addError 'email', 'Invalid email format.'

        errors if errors.length

TomatoBreak.User = User